package com.example.myapplication;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import static android.widget.Toast.LENGTH_LONG;

public class MainActivity extends AppCompatActivity {
    private static final int A = 1;
    private AlertDialog ad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS},
                    1);
        }

    }
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            doNext(requestCode,grantResults);


    }

    private void doNext(int requestCode, int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.i("----------","同意了");
                    // Permission Granted
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED) {
                        //申请WRITE_EXTERNAL_STORAGE权限
                        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                2);
                    }
                } else {
                    ad=new AlertDialog(MainActivity.this);
                    ad.setTitle("标题");
                    ad.setMessage("容内容");
                    ad.setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            ad.dismiss();
                            Intent intent =  new Intent(Settings.ACTION_APPLICATION_SETTINGS);
                            startActivity(intent);
                        }
                    });
                    ad.setNegativeButton("取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        // TODO Auto-generated method stub
                            ad.dismiss();
                            finish();
                        }
                    });

                }
break;
            case 2:
                if (grantResults.length>0&&grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.i("----------","同意了");
                } else {
                    Log.i("----------","bu 同意了");
                }
                break;
        }
    }

    }
